<!DOCTYPE html>
<%@ page import="java.util.*,rubix.model.*,rubix.controller.*,java.text.*,rubix.model.Po" %>
<%@include file ="masterPage.jsp" %>

<%
    String poId = (String)session.getAttribute("poID");
    String anotherPoId = request.getParameter("poID");    
%>

<html>
    <head>
        <title>KMP Fast East Pte Ltd - ERP Application</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
        <link rel="stylesheet" href="/resources/demos/style.css" />
        <script>
            $(function() {
                $("#datepicker").datepicker();
            });
        </script>
    </head>
    <body>
        <%
            if(poId != null) {
                Po po = PoManager.retrievePo(poId);
                String supplierId = po.getSupplierId();
                Supplier supplier = SupplierManager.retrieveSupplier(supplierId);
                String purchaseDate = po.getPurchaseDate();
                String day = purchaseDate.substring(0,2);
                String month = purchaseDate.substring(2,4);
                String year = purchaseDate.substring(4);
                String formatPurchaseDate = day + "/" + month + "/" + year;
                String partPrice = po.getPartPrice();
                double tempPrice = 0;
                try {
                    tempPrice = Double.parseDouble(partPrice);
                } catch(NumberFormatException e) { }
                DecimalFormat df2 = new DecimalFormat("##.00");
                double tempAmount = po.getOnOrderQuantity() * tempPrice;
                double totalAmount = Double.valueOf(df2.format(tempAmount)); 
        %>
        <div id="page-wrapper">
            <div class="col-lg-12">
                <div class="panel panel-primary">
                    <div class="panel-heading">  
                    <form name="updatePO" method="post" action="poController">                        
                        <h1 class="panel-title"><i class="icon-file-text-alt"></i> Update Purchase Order</h1><br>
                        <a href="purchaseOrderDetails.jsp"><button type="button" class="btn btn-danger"><i class="icon-save"></i> Save Changes</button></a>
                        <button name="delete" class="btn btn-danger" type="submit">Cancel Order</button>
                    </div>

                    <form name="updatePO" method="post" action="poController">
                    <div class="panel-body"> 
                        <div class="table-responsive">
                            <table class="table table-condensed table-hover table-striped tablesorter">                                
                                <thead>
                                    <h1>Purchase Order: <%=poId%></h1>
                                    <tr>
                                        <th>Supplier</th>
                                        <th>Date </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td><address><strong><%=supplier.getSupplierId()%></strong><br>
                                            <%=supplier.getSupplierDescription()%><br>
                                            <%=supplier.getSupplierLocation()%><br></td>
                                        <td><%=formatPurchaseDate%></address></td>
                                    </tr>
                                    <tr>
                                </tbody>
                            </table>
                            <table class="table table-bordered table-hover table-striped tablesorter">
                                <thead>
                                    <tr class="success">
                                        <th>Item ID</th>
                                        <th>Part ID</th>
                                        <th>Quantity</th>
                                        <th>Unit Price</th>
                                        <th>Sub-Total</th>
                                        <th>Update - Delete</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td><%=po.getItemId()%></td>
                                        <td><%=po.getPartId()%></td>
                                        <td><input type="text" name="Quantity" class="form-control" value="<%=po.getOnOrderQuantity()%>"></td>
                                        <td><%=po.getPartPrice()%></td>
                                        <td><%=totalAmount%></td>
                                        <td><button name="edit" class="btn btn-success" type="submit">Edit</button>
                                            <%--<button name="delete" class="btn btn-danger" type="submit">Delete</button></td>--%>
                                    </tr>
                                </tbody>
                            </table>
                                        
                            <div class="col-lg-13">
                                <table class="table table-condensed">
                                    <thead>
                                        <tr>
                                            <th class="active" colspan="8">Item Total</th>
                                            <td>1,645.60</td>
                                        </tr>
                                        <tr>                                     
                                            <th class="active" colspan="8">Tax</th>
                                            <td>0.00</td>
                                        </tr>
                                        <tr>                                                                             
                                            <th class="active" colspan="8">Discount</th>
                                            <td>-0.00</td> 
                                        </tr>
                                        <tr>                                                                             
                                            <th class="danger" colspan="8">Order Total</th>
                                            <td>USD 1,645.60</td> 
                                        </tr>
                                    </thead>                              
                                </table>
                            </div>
                            <form role="form">                        
                                <textarea class="form-control" rows="3" placeholder="Term and conditions..."></textarea>
                            </form>  
                        </div>
                    </div>
                </div>
            </div>
        </div>
    <% } else { %>
        <%
            Po po = PoManager.retrievePo(anotherPoId);
            String supplierId = po.getSupplierId();
            Supplier supplier = SupplierManager.retrieveSupplier(supplierId);
            String purchaseDate = po.getPurchaseDate();
            String day = purchaseDate.substring(0,2);
            String month = purchaseDate.substring(2,4);
            String year = purchaseDate.substring(4);
            String formatPurchaseDate = day + "/" + month + "/" + year;
            String partPrice = po.getPartPrice();
            double tempPrice = 0;
            try {
                tempPrice = Double.parseDouble(partPrice);
            } catch(NumberFormatException e) { }
            DecimalFormat df2 = new DecimalFormat("##.00");
            double tempAmount = po.getOnOrderQuantity() * tempPrice;
            double totalAmount = Double.valueOf(df2.format(tempAmount)); 
        %>
        <div id="page-wrapper">
            <div class="col-lg-12">
                <div class="panel panel-primary">
                    <div class="panel-heading">  
                    <form name="updatePO" method="post" action="poController">                        
                        <h1 class="panel-title"><i class="icon-file-text-alt"></i> Update Purchase Order</h1><br>
                        <a href="purchaseOrderDetails.jsp"><button type="button" class="btn btn-danger"><i class="icon-save"></i> Save Changes</button></a>
                        <button name="delete" class="btn btn-danger" type="submit">Cancel Order</button>
                    </div>

                    <form name="updatePO" method="post" action="poController">
                    <div class="panel-body"> 
                        <div class="table-responsive">
                            <table class="table table-condensed table-hover table-striped tablesorter">                                
                                <thead>
                                    <h1>Purchase Order: <%=poId%></h1>
                                    <tr>
                                        <th>Supplier</th>
                                        <th>Date </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td><address><strong><%=supplier.getSupplierId()%></strong><br>
                                            <%=supplier.getSupplierDescription()%><br>
                                            <%=supplier.getSupplierLocation()%><br></td>
                                        <td><%=formatPurchaseDate%></address></td>
                                    </tr>
                                    <tr>
                                </tbody>
                            </table>
                            <table class="table table-bordered table-hover table-striped tablesorter">
                                <thead>
                                    <tr class="success">
                                        <th>Item ID</th>
                                        <th>Part ID</th>
                                        <th>Quantity</th>
                                        <th>Unit Price</th>
                                        <th>Sub-Total</th>
                                        <th>Update - Delete</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td><%=po.getItemId()%></td>
                                        <td><%=po.getPartId()%></td>
                                        <td><input type="text" name="Quantity" class="form-control" value="<%=po.getOnOrderQuantity()%>"></td>
                                        <td><%=po.getPartPrice()%></td>
                                        <td><%=totalAmount%></td>
                                        <td><button name="edit" class="btn btn-success" type="submit">Edit</button>
                                            <%--<button name="delete" class="btn btn-danger" type="submit">Delete</button></td>--%>
                                    </tr>
                                </tbody>
                            </table>
                                        
                            <div class="col-lg-13">
                                <table class="table table-condensed">
                                    <thead>
                                        <tr>
                                            <th class="active" colspan="8">Item Total</th>
                                            <td>1,645.60</td>
                                        </tr>
                                        <tr>                                     
                                            <th class="active" colspan="8">Tax</th>
                                            <td>0.00</td>
                                        </tr>
                                        <tr>                                                                             
                                            <th class="active" colspan="8">Discount</th>
                                            <td>-0.00</td> 
                                        </tr>
                                        <tr>                                                                             
                                            <th class="danger" colspan="8">Order Total</th>
                                            <td>USD 1,645.60</td> 
                                        </tr>
                                    </thead>                              
                                </table>
                            </div>
                            <form role="form">                        
                                <textarea class="form-control" rows="3" placeholder="Term and conditions..."></textarea>
                            </form>  
                        </div>
                    </div>
                </div>
            </div>
        </div>    
    <% } %>
    
    </body>
</html>